package cn.bdqn.biz_consumer.controller;

import cn.bdqn.common.Constants;
import cn.bdqn.common.Dto;
import cn.bdqn.common.DtoUtil;
import cn.bdqn.service.RpcArticleService;
import cn.bdqn.service.RpcDirectoryService;
import cn.bdqn.vo.ArticleListSearchVO;
import cn.bdqn.vo.ArticleListVO;

import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 * 文章相关功能controller
 */
@Controller
@RequestMapping("/article")
public class ArticleController {

    @Reference
    private RpcDirectoryService rpcDirectoryService;
    @Reference
    private RpcArticleService rpcArticleService;

    /***
     * 文章首页列表显示
     * @return
     */
    @ApiOperation(value = "文章首页列表显示", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "文章首页列表显示 ")
    @RequestMapping(value = "/articleIndex", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto articleIndex() {
        try {
            return DtoUtil.returnDataSuccess(rpcDirectoryService.findArticleTag());
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败", Constants.Article.QUERY_FAIL);
        }
    }

    /***
     * 文章查询
     * @param articleListSearchVO 查询条件
     * @return
     */
    @ApiOperation(value = "文章查询", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "文章查询 ")
    @RequestMapping(value = "/articleList", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto articleList(@RequestBody ArticleListSearchVO articleListSearchVO) {
        if (null == articleListSearchVO || null == articleListSearchVO.getClassify_first())
            return DtoUtil.returnFail("查询失败", Constants.Article.QUERY_FAIL);
        try {
            List<ArticleListVO> list = rpcArticleService.findArticleByQuery(articleListSearchVO);
            if (null == list.get(0))
                return DtoUtil.returnFail("暂无相关文章", Constants.Article.QUERY_ARTICLE_NOT);
            return DtoUtil.returnDataSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(), Constants.SYSTEM_ERROR);
        }
    }

    @RequestMapping(value = "/articleInfo/{articleId}", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto articleInfo(@PathVariable Integer articleId) {
        if (null == articleId || articleId == 0)
            return DtoUtil.returnFail("查询失败", Constants.Article.QUERY_FAIL);

        return null;
    }
}
